package day10;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class homework03 {
    //    作业3:拷贝一个文件夹，考虑子文件
//    File src=new File(“d:\\aa\\src”);
//    File dest=new File(“d:\\aaa\\dest”);
//    Copydir(src,dest);
    public static void main(String[] args) throws Exception {
        File src = new File("d:\\aaa\\src");
        File dest = new File("d:\\aaa\\dest");
        Copydir(src, dest);
    }

    public static void Copydir(File src, File dest) throws Exception {
        File[] files = src.listFiles();
        if (files != null) {
            for (File file : files) {
                {
                    if (file.isDirectory()){
                        File newFile = new File(dest, file.getName());
                        newFile.mkdir();
                        Copydir(file,newFile);
                    }if (file.isFile()){
                        FileInputStream fis=new FileInputStream(file);
                        FileOutputStream fos=new FileOutputStream(dest.getPath()+"\\"+file.getName());
                        byte[] bytes=new byte[1024];
                        int len=0;
                        while((len=fis.read(bytes))!=-1){
                            fos.write(bytes,0,len);
                    }
                    }
                }
            }
        }
    }
}